package au.edu.swin.videoshop.publicapp.ui.sections;


import java.io.Serializable;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.component.UIData;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;

import au.edu.swin.videoshop.bean.Title;
import au.edu.swin.videoshop.bean.TitleCategory;
import au.edu.swin.videoshop.bean.TitleMedium;
import au.edu.swin.videoshop.service.TitleService;

public class TitleSection implements Serializable {
	private List mTitles;
	private List mCategories;
	private TitleCategory mSelectedCategory;
	private Title mSelectedTitle;
	
	@EJB
	private TitleService mTitleService;
	
	public TitleSection() {
	}

	protected void sortWithComparator(Comparator comparator) {
		Collections.sort(mTitles, comparator);
	}

	public String sortByYear() {
		sortWithComparator(new Title.YearComparator());
		return null;
	}

	public String sortByTitle() {
		sortWithComparator(new Title.TitleNameComparator());
		return null;
	}
	
	public String sortByClassification() {
		sortWithComparator(new Title.ClassificationComparator());
		return null;
	}

	/**
	 * @return the titles
	 */
	public List getTitles() {
		return mTitles;
	}

	/**
	 * @return the categories
	 */
	public List getCategories() {
		if (mCategories == null) {
			mCategories = mTitleService.findAllCategories();
		}
		return mCategories;
	}

	/**
	 * @return the selectedCategory
	 */
	public TitleCategory getSelectedCategory() {
		return mSelectedCategory;
	}
	
	/**
	 * @return the selectedTitle
	 */
	public Title getSelectedTitle() {
		return mSelectedTitle;
	}

	/**
	 * @param selectedTitle the selectedTitle to set
	 */
	public void setSelectedTitle(Title selectedTitle) {
		mSelectedTitle = selectedTitle;
	}

	public String reserve() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		UIViewRoot root = facesContext.getViewRoot();
		UIData table = (UIData)root.findComponent("reservationForm").findComponent("reservationTable");
		
		TitleMedium medium = (TitleMedium) table.getRowData();
		
		ReservationSection reservationSection = (ReservationSection)facesContext.getApplication().evaluateExpressionGet(facesContext, "#{reservationSection}", ReservationSection.class);

		reservationSection.setSelectedMedium(medium);
		
		return "reserve";
	}

	public String selectCategory() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		UIViewRoot root = facesContext.getViewRoot();
		UIData table = (UIData)root.findComponent("categorySection").findComponent("categoryForm").findComponent("categoryTable");
		
		mSelectedCategory = (TitleCategory) table.getRowData();
		
		TitleSearchSection titleSearchSection = (TitleSearchSection)facesContext.getApplication().evaluateExpressionGet(facesContext, "#{searchSection}", TitleSearchSection.class);
		titleSearchSection.getSearchCriteria().setCategoryId(mSelectedCategory.getTitleCategoryId());
		
		mTitles = mTitleService.findTitlesByCategoryId((mSelectedCategory != null?mSelectedCategory.getTitleCategoryId():null));
		
		return "browse";
	}

	/**
	 * Sets up the detail page by getting the bean backing the current row in
	 * the UIData table and passing it to the detail page.
	 * 
	 * @return the navigation outcome.
	 */
	public String details() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		UIViewRoot root = facesContext.getViewRoot();
		UIData table = (UIData)root.findComponent("titleForm").findComponent("titleTable");
		mSelectedTitle = (Title) table.getRowData();
	
		return "details";
	}
}
